package com.toe.commodity.webapi.controller;

import com.toe.commodity.service.CommodityClassesService;
import com.toe.commodity.service.CommodityPictureService;
import com.toe.common.restful.JsonPage;
import com.toe.common.restful.JsonResult;
import com.toe.pojo.dto.commodity.CommodityClassesDTO;
import com.toe.pojo.entity.commodity.CommodityClasses;
import com.toe.pojo.vo.commodity.CommodityClassVO;
import com.toe.pojo.vo.commodity.CommodityUserBuyClassesVO;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 陶涛
 * @since 2022-07-19
 */
@RestController
@RequestMapping("/webapi/commodity-classes")
public class CommodityClassesController {

    @Autowired
    CommodityClassesService commodityClassesService;
    @Autowired
    CommodityPictureService commodityPictureService;

    @PostMapping("/addNewClassCommodity")
    @ApiOperation("新增商品")
    public JsonResult addNewCommodity(CommodityClasses commodityClasses, @RequestParam("file") MultipartFile mpfile) {
        //图片上传
        String storeUrlPath = commodityPictureService.pictureUpload(mpfile);
        commodityClasses.setCover(storeUrlPath);
        commodityClassesService.addNewCommodityClass(commodityClasses);
        return JsonResult.ok("添加完成");
    }

    @PostMapping("/updateClassCommodity")
    @ApiOperation("修改商品信息")
    public JsonResult updateCommodity(@RequestBody CommodityClassesDTO commodityClassesDTO, @RequestParam("file") MultipartFile mpfile) {
        String storeUrlPath = commodityPictureService.pictureUpload(mpfile);
        commodityClassesDTO.setCover(storeUrlPath);
        commodityClassesService.updateCommodityClass(commodityClassesDTO);
        return JsonResult.ok("添加完成");
    }

    @GetMapping("/deletedCommodityClassById/{id}")
    @ApiOperation("根据商品id删除商品")
    public JsonResult deleteCommodity(@PathVariable("id") Long id) {
        commodityClassesService.deletedCommodityClass(id);
        return JsonResult.ok("删除完成");
    }

    @GetMapping("/get/{id}/{userId}")
    @ApiOperation("根据商品id查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", name = "id", example = "1"),
            @ApiImplicitParam(value = "用户id", name = "userId", example = "10")
    })
    /*同步事务*/
    public JsonResult getCommodityClassById(@PathVariable("id") Long id,@PathVariable("userId") Long userId){
        CommodityClassVO commodityClass= commodityClassesService.getCommodityClassById(id,userId);
        return JsonResult.ok("查询成功",commodityClass);

    }

    @GetMapping("/getCommodityClassBySales/{pageNum}/{pageSize}")
    @ApiOperation("根据商品销量查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
    })
    public JsonResult getCommodityBySale(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize) {
        JsonPage<CommodityClassVO> jsonPage = commodityClassesService.getCommodityClassBySales(pageNum, pageSize);
        return JsonResult.ok("查询完成", jsonPage);
    }

    @GetMapping("/getCommodityClassByType/{pageNum}/{pageSize}/{id}")
    @ApiOperation("根据商品类型查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
    })
    public JsonResult getCommodityByType(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,@PathVariable("id") Long id) {
        JsonPage<CommodityClassVO> jsonPage = commodityClassesService.getCommodityClassByClassTypeId(pageNum, pageSize, id);
        return JsonResult.ok("查询完成", jsonPage);
    }

    @GetMapping("/getCommodityClassByBusiness/{pageNum}/{pageSize}/{id}")
    @ApiOperation("根据商家查询商品")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10")
    })
    public JsonResult getCommodityByBusiness(@PathVariable("pageNum") Integer pageNum,@PathVariable("pageSize") Integer pageSize,@PathVariable("id") Long id) {
        JsonPage<CommodityClassVO> jsonPage = commodityClassesService.getCommodityClassByBusinessId(pageNum, pageSize, id);
        return JsonResult.ok("查询完成", jsonPage);
    }

    @GetMapping("/buy-classes/{pageNum}/{pageSize}/{userId}")
    @ApiOperation("查询该用户已购买的课程")
    @ApiImplicitParams({
            @ApiImplicitParam(value = "用户id", name = "userId", example = "30"),
            @ApiImplicitParam(value = "页码", name = "pageNum", example = "1"),
            @ApiImplicitParam(value = "每页条数", name = "pageSize", example = "10"),
    })
    public JsonResult<JsonPage<CommodityUserBuyClassesVO>> getUserHaveBuyClasses(
            @PathVariable("userId") Long userId,
            @PathVariable("pageNum") Integer pageNum,
            @PathVariable("pageSize") Integer pageSize
    ) {
        return JsonResult.ok(commodityClassesService
                .listUserBuyClassesByIds(userId, pageNum, pageSize));
    }

}

